#include "login.h"

//返回值-1，表示不存在 -2表示密码错误,0表示登陆成功
int judge_login(int user_id, char *password, MYSQL *mysql_con){

    char buf[1024] = { 0 };
    sprintf(buf, "select user_status, user_password, salt from user where user_id = %d", user_id);
    //mysql_query该函数成功返回0，不成功返回非0
    if (mysql_query(mysql_con, buf)) {
        printf("%s\n", mysql_error(mysql_con));
        return -1;
    }

    MYSQL_RES *result;
    MYSQL_ROW row;

    result = mysql_store_result(mysql_con);
    if(!result || (mysql_num_rows(result) == 0)){
        return -1;
    }

    row = mysql_fetch_row(result);
    mysql_free_result(result);

    if(strcmp("-1", row[0]) == 0){
        //表示该用户状态为-1，已经注销了该账号，不能使用
        return -1;
    }
    if(strcmp(crypt(password, row[2]), row[1]) == 0){
        return 0;
    }
    return -2;
}
